package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.opengl.ETC1;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private EditText nameText;
    private EditText ageText;
    private EditText heightText;
    private Button saveButton;
    private Button readButton;
    private final String FILE_NAME = "SaveSetting";
    public static int MODE = Context.MODE_PRIVATE;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nameText = findViewById(R.id.editText_name);
        ageText = findViewById(R.id.editText_age);
        heightText = findViewById(R.id.editText_height);

    }
    @Override
    protected void onStart() {
        super.onStart();
//        loadSharedPreferences();
    }

    @Override
    protected void onStop() {
        super.onStop();
//        saveSharedPreferences();
    }

    private void loadSharedPreferences(){
        SharedPreferences preferences = getSharedPreferences(FILE_NAME, MODE);
        String name = preferences.getString("name", "dylan");
        int age = preferences.getInt("age", 18);
        float height = preferences.getFloat("height", 1.81f);
        nameText.setText(name);
        ageText.setText(String.valueOf(age));
        heightText.setText(String.valueOf(height));
    }
    private void saveSharedPreferences(){
        String name = nameText.getText().toString();
        int age = Integer.parseInt(ageText.getText().toString());
        float height = Float.parseFloat(heightText.getText().toString());
        SharedPreferences preferences = getSharedPreferences(FILE_NAME, MODE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString("name", name);
        edit.putInt("age", age);
        edit.putFloat("height", height);
        edit.commit();
    }
}